package com.weactivity.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.weactivity.dto.ActivitySimpleDTO;
import com.weactivity.dto.UARDTO;
import com.weactivity.entity.Activity;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ActivityMapper extends BaseMapper<Activity> {

    List<ActivitySimpleDTO> findById(Long userId);

    @Select("select * from activity where id = #{activityId} ;")
    Activity findSingleByid(int activityId);

    List<Activity> selectAllActivity(int userId);

    List<Activity> selectValidActivity(int userId);

    /**
     * 用户在接收消息界面时通过该接口参加活动
     * 使用uid, aid 和默认的权限插入user_activity_role表
     *
     * @param userId
     * @param activityId
     * @return
     */
    @Insert("insert into user_activity_role (uid,aid,power,role) values (#{userId},#{activityId},2,2);")
    int insertUseUidAndAid(@Param("userId") int userId,
                           @Param("activityId") int activityId
    );

    /**
     * 向activity_organizer表插入活动组织者
     * @param uid
     * @param aid
     * @return
     */

    @Insert("insert into activity_organizer (uid,aid,power,role) values (#{uid},#{aid},0,0);")
    int insertOrganizer(@Param("uid") int uid,
                        @Param("aid") int aid);

    /**
     * 返回操作数，并且输入的activity对象的id值被填充为后端生成的id
     * @param activity
     * @return
     */
    int insertAct(Activity activity);

    @Select("select am.uid from activity_manager am where am.aid = #{aid} ;")
    List<Integer> findManagerIdsByAid(@Param("aid") int aid);

    @Select("select * from activity where id = #{keyword} or name = #{keyword}")
    Activity searchActInfo(String keyword);

    @Select("SELECT MIN(role) FROM user_activity_role WHERE uid = #{id} AND aid = #{aid};")
    Integer findRole(@Param("aid") int aid,@Param("id") Long id);

    @Insert("insert into activity_manager (uid,aid,power,role) values (#{aid},#{id},1,1);")
    int setManager(@Param("aid") int aid,@Param("id") Long id);

    @Delete("delete  from activity_manager where uid = #{uid} and aid = #{aid};")
    int deleteManager(@Param("uid") int uid, @Param("aid") int aid);

    @Select("select uid from activity_organizer where aid = #{id};")
    int findOrganizerByid(int id);

    @Select("select aid from user_activity_role where uid = #{i};")
    List<Integer> selectPartActId(Integer i);


    List<Activity> selectActByList(@Param("ids") List<Integer> activityIdList);

    @Select("select count(*) from user_activity_role where aid = #{id} ")
    int countPart(int id);

    @Insert("insert into activity_manager (uid,aid,power,role) values (#{uid},#{aid},1,1);")
    int insertManagerById(@Param("uid") Long uid,@Param("aid") int aid);

    @Insert("insert into user_activity_role (uid,aid,power,role) values (#{uid},#{aid},1,1);")
    int insertManagerIntoUARById(@Param("uid") Long uid,@Param("aid") int aid);

    @Insert("insert into activity_participants (uid,aid,power,role) values (#{uid},#{aid},2,2);")
    int insertPartById(@Param("uid") Integer i,@Param("aid") int id);


    @Insert("insert into user_activity_role (uid,aid,power,role) values (#{uid},#{aid},2,2);")
    int insertPartIntoUARById(@Param("uid") Integer i,@Param("aid") int id);


    @Delete("delete from user_activity_role where aid = #{aid} and uid = #{uid} ;")
    int deleteFromUARByAidAndUid(@Param("aid") int id,@Param("uid") Integer userId);

    @Delete("delete from activity_participants where aid = #{aid} and uid = #{uid} ;")
    int deleteFromPartByAidAndUid(@Param("aid") int id, @Param("uid") Integer userId);

    @Select("select uid,power from user_activity_role where aid = #{aid};")
    List<UARDTO> selectAllUser(int aid);
}
